package com.jinbiao.quasar.scheduling.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jinbiao.quasar.model.SysJob;
import org.noear.solon.Solon;
import org.noear.solon.Utils;
import com.jinbiao.quasar.scheduling.ScheduledAnno;

import java.util.Date;
import java.util.Properties;

/**
 * 调整助手
 *
 * @author noear
 * @since 1.11
 */
public class ScheduledHelper {
    public static void resetScheduled(ScheduledAnno warpper) {
        if (warpper.cron().length() < 6 || warpper.cron().indexOf(" ") < 0) {
            warpper.fixedRate(fixedRate(warpper.cron()));
            warpper.cron("");
        } else {
            warpper.fixedRate(0L);
        }
    }

    /**
     * 配置加持
     */
    public static void configScheduled(ScheduledAnno warpper, SysJob job) {
        if (warpper.cron().length() < 6 || warpper.cron().indexOf(" ") < 0) {
            /*if (warpper.fixedRate() == 0) {
                warpper.fixedRate(Long.parseLong(warpper.cron()));
            }*/
            warpper.cron("");
        }

        /*if (Utils.isNotEmpty(warpper.name())) {
            Properties prop = Solon.cfg().getProp("solon.scheduling.job." + warpper.name());

            if (prop.size() > 0) {
                String enableStr = prop.getProperty("enable");

                String cronStr = prop.getProperty("cron");
                String zoneStr = prop.getProperty("zone");

                String fixedRateStr = prop.getProperty("fixedRate");
                String fixedDelayStr = prop.getProperty("fixedDelay");
                String initialDelayStr = prop.getProperty("initialDelay");

                if ("false".equals(enableStr)) {
                    warpper.enable(false);
                }

                if (Utils.isNotEmpty(cronStr)) {
                    if (cronStr.length() > 6 && cronStr.contains(" ")) {
                        warpper.cron(cronStr);
                    } else {
                        warpper.fixedRate(fixedRate(cronStr));
                    }
                }

                if(Utils.isNotEmpty(zoneStr)){
                    warpper.zone(zoneStr);
                }

                if (Utils.isNotEmpty(fixedRateStr)) {
                    warpper.fixedRate(Long.parseLong(fixedRateStr));
                }

                if (Utils.isNotEmpty(fixedDelayStr)) {
                    warpper.fixedDelay(Long.parseLong(fixedDelayStr));
                }

                if (Utils.isNotEmpty(initialDelayStr)) {
                    warpper.initialDelay(Long.parseLong(initialDelayStr));
                }
            }
        }*/
        if (job != null) {

            String enableStr = "onOff_on".equals(job.getStatus()) ? "true" : "false";

            String cronStr = job.getCron();
            String zoneStr = job.getZone();

            String fixedRateStr = job.getFixedRate();
            String fixedDelayStr = job.getFixedDelay();
            String initialDelayStr = job.getInitialDelay();
            Date startTime = job.getStartTime();
            String misfirePolicy = job.getMisfirePolicy();
            String jobGroup = job.getJobGroup();
            String jobName = job.getJobInnerName();
            String jobNickName = job.getJobName();
            if ("false".equals(enableStr)) {
                warpper.enable(false);
            }else{
                warpper.enable(true);
            }

            if (Utils.isNotEmpty(cronStr)) {
                if (cronStr.length() > 6 && cronStr.contains(" ")) {
                    warpper.cron(cronStr);
                }
            }
            if (Utils.isNotEmpty(jobName)) {
                warpper.name(jobName);
            }
            if (Utils.isNotEmpty(jobNickName)) {
                warpper.nickName(jobNickName);
            }

            if (Utils.isNotEmpty(fixedRateStr)) {
                warpper.fixedRate(Long.parseLong(fixedRateStr)*1000);
            }

            if (Utils.isNotEmpty(fixedDelayStr)) {
                warpper.fixedDelay(Long.parseLong(fixedDelayStr)*1000);
            }

            if (Utils.isNotEmpty(initialDelayStr)) {
                warpper.initialDelay(Long.parseLong(initialDelayStr)*1000);
            }

            if (Utils.isNotEmpty(misfirePolicy)) {
                warpper.misfirePolicy(misfirePolicy);
            }

            if (startTime != null) {
                warpper.startTime(DateUtil.format(startTime, DatePattern.NORM_DATETIME_PATTERN));
            }

            if(StrUtil.isNotEmpty(jobGroup)){
                warpper.jobGroup(jobGroup);
            }
        }
    }

    public static long fixedRate(String cronx) {
        if (cronx.endsWith("ms")) {
            long period = Long.parseLong(cronx.substring(0, cronx.length() - 2));
            return period;
        } else if (cronx.endsWith("s")) {
            long period = Long.parseLong(cronx.substring(0, cronx.length() - 1));
            return period * 1000;
        } else if (cronx.endsWith("m")) {
            long period = Long.parseLong(cronx.substring(0, cronx.length() - 1));
            return period * 1000 * 60;
        } else if (cronx.endsWith("h")) {
            long period = Long.parseLong(cronx.substring(0, cronx.length() - 1));
            return period * 1000 * 60 * 60;
        } else if (cronx.endsWith("d")) {
            long period = Long.parseLong(cronx.substring(0, cronx.length() - 1));
            return period * 1000 * 60 * 60 * 24;
        } else {
            return 0L;
        }
    }
}
